package teacher.chapter07;//package com.itheima;

import java.util.Comparator;
        import java.util.TreeSet;
class Student4{                             //创建Student4类
    private String id;
    private String name;
    public Student4(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    // 重写toString()方法
    public String toString() {
        return id + ":" + name;
    }
}
public  class Example13 {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {  //重写
                Student4 stu1= (Student4)o1;
                Student4 stu2= (Student4)o2;
                if(!stu1.getId().equals(stu2.getId())){
                    return  stu1.getId().compareTo(stu2.getId());
                }
                else{
                    return  stu1.getName().compareTo(stu2.getName());
                }
            }
        });
        ts.add(new Student4("2", "Mary"));//向ts集合中添加元素
        ts.add(new Student4("1","Jack"));
        ts.add(new Student4("3", "Lisa"));
        ts.add(new Student4("2", "Lily"));
        System.out.println(ts);
    }
}